package cn.itsource.ymcc.config;

import cn.itsource.ymcc.domain.Permission;
import cn.itsource.ymcc.mapper.PermissionMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.annotation.web.configurers.ExpressionUrlAuthorizationConfigurer;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;

import java.util.List;

@Configuration//表示当前类是一个配置类
@EnableWebSecurity//启动 安全 配置
@EnableGlobalMethodSecurity(securedEnabled = true,prePostEnabled= true) //开启权限的注解支持
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
    //提供用户信息，这里没有从数据库查询用户信息，在内存中模拟
    @Autowired
    private PermissionMapper permissionMapper;

    //密码编码器：不加密
    @Bean
    public PasswordEncoder passwordEncoder(){
        return new BCryptPasswordEncoder();
    }

    //配置认证管理器，授权模式为“poassword”时会用到
    @Bean
    public AuthenticationManager authenticationManager() throws Exception {
        return super.authenticationManager();
    }

    /**
     * 1这个配置不需要你去写
     * 2但是你要看的懂
     * 3并且 你要学会修改
     * @param http
     * @throws Exception
     */
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        ExpressionUrlAuthorizationConfigurer<HttpSecurity>.ExpressionInterceptUrlRegistry expressionInterceptUrlRegistry2 = http.formLogin()                              //允许表单登录
                .successForwardUrl("/loginSuccess")             // 设置登陆成功页
                .and()
                .authorizeRequests();
        //要放行的路径没有做动态
        ExpressionUrlAuthorizationConfigurer<HttpSecurity>.ExpressionInterceptUrlRegistry expressionInterceptUrlRegistry = expressionInterceptUrlRegistry2                                //授权配置
                .antMatchers("login","/loginSuccess").permitAll();

        //需要特殊校验
        ExpressionUrlAuthorizationConfigurer<HttpSecurity>.ExpressionInterceptUrlRegistry exp = null;
        List<Permission> permissions = permissionMapper.selectList(null);
        for (Permission permission : permissions) {
            exp = expressionInterceptUrlRegistry  //登录路径放行
                    .antMatchers(permission.getResource()).hasAuthority(permission.getSn());
        }

        exp.anyRequest().authenticated()                   //其他路径都要认证之后才能访问
            .and().logout().permitAll()                    //登出路径放行 /logout
            .and().csrf().disable();                        //关闭跨域伪造检查  可以提供效率  自己下去研究
    }
}
